<?php
/**
 * 分页导航栏
 */
function _ctlPagenav($name, $attribs){
    $opts = array('pager', 'controller', 'action', 'length', 'slider', 'prevLabel', 'nextLabel');
    $data = FLEA_WebControls::extractAttribs($attribs, $opts);

    if ($data['slider'] <= 0) { $data['slider'] = 1; }
    if ($data['length'] <= 0) { $data['length'] = 5; }
    if ($data['prevLabel'] == '') { $data['prevLabel'] = '&laquo;'; }
    if ($data['nextLabel'] == '') { $data['nextLabel'] = '&raquo;'; }

    $beginRow = $data['pager']['pageSize'] * $data['pager']['currentPage'] + 1;
    $endRow = $data['pager']['pageSize'] * ($data['pager']['currentPage'] + 1);

    $output = "<p class=\"pager\" id=\"{$name}\">";

    $output .= "<span class=\"displaying_num\">Displaying {$beginRow}&#8211;{$endRow} of {$data['pager']['count']}</span>";

    if ($data['pager']['currentPage'] == $data['pager']['firstPage']) {
        $output .= "<span class=\"disabled\">{$data['prevLabel']}</span>";
    } else {
        $attribs['page'] = $data['pager']['prevPage'];
        $url = url($data['controller'], $data['action'], $attribs);
        $output .= "<a href=\"{$url}\">{$data['prevLabel']}</a>";
    }

    $base = $data['pager']['firstPage'];
    $currentPage = $data['pager']['currentPage'];

    $mid = intval($data['length'] / 2);
    if ($currentPage < $data['pager']['firstPage']) {
        $currentPage = $data['pager']['firstPage'];
    }
    if ($currentPage > $data['pager']['lastPage']) {
        $currentPage = $data['pager']['lastPage'];
    }

    $begin = $currentPage - $mid;
    if ($begin < $data['pager']['firstPage']) {
        $begin = $data['pager']['firstPage'];
    }

    $end = $begin + $data['length'] - 1;
    if ($end >= $data['pager']['lastPage']) {
        $end = $data['pager']['lastPage'];
        $begin = $end - $data['length'] + 1;
        if ($begin < $data['pager']['firstPage']) {
            $begin = $data['pager']['firstPage'];
        }
    }

    if ($begin > $data['pager']['firstPage']) {
        for ($i = $data['pager']['firstPage']; $i < $data['pager']['firstPage'] + $data['slider'] && $i < $begin; $i++) {
            $attribs['page'] = $i;
            $in = $i + 1 - $base;
            $url = url($data['controller'], $data['action'], $attribs);
            $output .= "<a href=\"{$url}\">{$in}</a>";
        }

        if ($i < $begin) {
            $output .= "<span class=\"dots\">...</span>";
        }
    }

    for ($i = $begin; $i <= $end; $i++) {
        $attribs['page'] = $i;
        $in = $i + 1 - $base;
        if ($i == $data['pager']['currentPage']) {
            $output .= "<span class=\"current\">{$in}</span>\n";
        } else {
            $url = url($data['controller'], $data['action'], $attribs);
            $output .= "<a href=\"{$url}\">{$in}</a>";
        }
    }

    if ($data['pager']['lastPage'] - $end > $data['slider']) {
        $output .= "<span class=\"dots\">...</span>";
        $end = $data['pager']['lastPage'] - $data['slider'];
    }

    for ($i = $end + 1; $i <= $data['pager']['lastPage']; $i++) {
        $attribs['page'] = $i;
        $in = $i + 1 - $base;
        $url = url($data['controller'], $data['action'], $attribs);
        $output .= "<a href=\"{$url}\">{$in}</a>";
    }

    if ($data['pager']['currentPage'] == $data['pager']['lastPage']) {
        $output .= "<span class=\"disabled\">{$data['nextLabel']}</span>";
    } else {
        $attribs['page'] = $data['pager']['nextPage'];
        $url = url($data['controller'], $data['action'], $attribs);
        $output .= "<a href=\"{$url}\">{$data['nextLabel']}</a>";
    }

    $output .= "</p>";

    return $output;
}